#!/usr/bin/perl

$dir     = "./";
$ihex = "build/telosb/main.ihex";

sub excmd() {
	my ($cmd) = @_;
	print "$cmd\n";
	$info = `$cmd`;
}

$info = `motelist > motelist.log`;
open fd, "<motelist.log" or die "cannot open motelist.log\n";
system("make telosb");
$id=1;
while (<fd>) {
	chomp;
	my @rs = split;
	if ($rs[0] eq "TB00308S") {
		my $port = $rs[1];
		my $cmd = "tos-set-symbols --objcopy msp430-objcopy --objdump msp430-objdump --target ihex $ihex $ihex-0.out TOS_NODE_ID=0 ActiveMessageAddressC__addr=0";
		print "$cmd\n";
		system($cmd);

		$cmd = "tos-bsl --telosb -c $port -r -e -I -p $ihex-0.out";
		print "$cmd\n";
		system("$cmd &");
		$id++;		
	} elsif ($rs[0] eq "TB00337S") { # base
		my $port = $rs[1];
		my $cmd = "tos-set-symbols --objcopy msp430-objcopy --objdump msp430-objdump --target ihex $ihex $ihex-1.out TOS_NODE_ID=1 ActiveMessageAddressC__addr=1";
                print "$cmd\n";
                system($cmd);

                $cmd = "tos-bsl --telosb -c $port -r -e -I -p $ihex-1.out";

		print "$cmd\n";
		system("$cmd &");
	}elsif ($rs[0] eq "TB00543S") { # base
		my $port = $rs[1];
		my $cmd = "tos-set-symbols --objcopy msp430-objcopy --objdump msp430-objdump --target ihex $ihex $ihex-2.out TOS_NODE_ID=2 ActiveMessageAddressC__addr=2";
                print "$cmd\n";
                system($cmd);

                $cmd = "tos-bsl --telosb -c $port -r -e -I -p $ihex-2.out";
		print "$cmd\n";
		system("$cmd &");
	}
}
close fd;

